package com.wlbsns.demo.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wlbsns.demo.entity.Demo;
import com.wlbsns.demo.service.ISysService;
import com.wlbsns.demo.service.SysServiceImpl;
import com.wlbsns.framework.exception.BusinessException;
import com.wlbsns.framework.exception.SystemException;
import com.wlbsns.framework.tools.DateUtils;

/**
 * Servlet implementation class UserServlet
 */
public class DemoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DemoServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		//获取表单提交数据
		String name = request.getParameter("name");
		String pwd = request.getParameter("password");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String dmoney = request.getParameter("money");
		
		//创建一个demo对象
		Demo demo = new Demo();
		
		//将数据封装到demo中
		demo.setDname(name);
		demo.setDpassword(pwd);
		demo.setDsex(sex);
		demo.setDbirthday(DateUtils.parseDate(birthday, ""));
		demo.setDmoney(Double.valueOf(dmoney));
		
		request.setAttribute("name", name);
		
		//实例化一个ISysService对象
		ISysService sDao = new SysServiceImpl();
		
		try {
			
			//调用添加对象方法
			boolean b = sDao.addObject(demo);
			
			
			if(b){
				request.getRequestDispatcher(request.getContextPath()+"index.jsp").forward(request, response);
			}else{
				System.out.println("出错了");
			}
			
		} catch (SystemException e) {
			e.printStackTrace();
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		
	}

}



